package com.mdp.workflow.ru.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.core.query.QueryTools;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;
import com.mdp.workflow.ru.entity.ProcinstNodeInfo;
import com.mdp.workflow.ru.service.ProcinstNodeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/**/ru/procinstNodeInfo")
@Api(tags={"mdp_ru_procinst_node_info-操作接口"})
public class ProcinstNodeInfoController {
	
	static Logger logger =LoggerFactory.getLogger(ProcinstNodeInfoController.class);
	
	@Autowired
	private ProcinstNodeInfoService procinstNodeInfoService;

	@ApiOperation( value = "mdp_ru_procinst_node_info-查询列表",notes=" ")
	@ApiEntityParams(ProcinstNodeInfo.class)
	@ApiResponses({
		@ApiResponse(code = 200,response=ProcinstNodeInfo.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listProcinstNodeInfo(@ApiIgnore @RequestParam Map<String,Object> params){
		try {
			User user=LoginUtils.getCurrentUserInfo();
			QueryWrapper<ProcinstNodeInfo> qw = QueryTools.initQueryWrapper(ProcinstNodeInfo.class , params);
			IPage page=QueryTools.initPage(params);
			List<Map<String,Object>> datas = procinstNodeInfoService.selectListMapByWhere(page,qw,params);
			return Result.ok("query-ok","查询成功").setData(datas).setTotal(page.getTotal());
		}catch (BizException e) {
			return Result.error(e);
		}catch (Exception e) {
			return Result.error(e);
		}
	}

	/**
	 * 请求,如list
	 * 分页参数 {currentPage:1,pageSize:10,total:0}
	 * 根据条件查询数据对象列表,如果不上传分页参数，将不会分页。后台自动分页，无需编程
	 */
	@RequestMapping(value="/getNodeInfos")
	public Result getNodeInfos( @RequestParam Map<String,Object> p){
		String procDefId=(String) p.get("procInstId");
		if(StringUtils.isEmpty(procDefId)) {
			return Result.error("procInstId参数必传");
		}else {
			List<ProcinstNodeInfo>	actAssigneeList = procinstNodeInfoService.getNodeInfosFromCache((String) p.get("procInstId"));//列出ActAssignee列表
			return Result.ok().setData(actAssigneeList);

		}

	}

	@ApiOperation( value = "批量更新nodeInfos",notes="批量更新nodeInfos")
	@ApiResponses({
			@ApiResponse(code = 200, message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'失败时错误码'}")
	})
	@RequestMapping(value="/updateNodeInfos",method=RequestMethod.POST)
	public Result updateNodeInfos(@RequestBody List<ProcinstNodeInfo> procinstNodeInfos) {

		if(procinstNodeInfos==null |procinstNodeInfos.size()==0) {
			return Result.error("数据为空，请上传数据");
		}else {
			if(StringUtils.isEmpty(procinstNodeInfos.get(0).getProcInstId())) {
				return Result.error("procInstId参数不能为空");
			}else {
				procinstNodeInfoService.updateNodeInfos(procinstNodeInfos.get(0).getProcInstId(),procinstNodeInfos);
				return Result.ok();
			}
		}
	}

}
